package fr.umlv.jee.jawp.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import fr.umlv.jee.jawp.persistence.dao.IProductDAO;
import fr.umlv.jee.jawp.persistence.object.Product;

@Service("productService")
public class ProductService {

	@Resource(name = "productDAO")
	private IProductDAO dao;

	/*************************************
	 * Creation
	 *************************************/

	@Transactional
	public Product createNewProduct(String ASIN, String name, int categoryId, int limit){
		return dao.createNewProduct(ASIN, name, categoryId, limit);
	}

	/*************************************
	 * Getter
	 *************************************/

  @Transactional(readOnly = true)
  public List<Product> getProducts(){
    return dao.getProducts();
  }
  
  @Transactional(readOnly = true)
  public List<Map<String, String>> getProductx(AmazonService amazon){
    System.err.println(amazon);
    List<Product> products = dao.getProducts();
    List<Map<String, String>> list = new ArrayList<Map<String,String>>();
    
    for(Product prod : products) {
      Map<String, String> map = amazon.findArticleByItemID(prod.getASIN());
      list.add(map);
    }
    
    return list;
  }

	@Transactional(readOnly = true)
	public List<Product> getProductsByCategory(int categoryId){
		return dao.getProductsByCategory(categoryId);
	}

	@Transactional(readOnly = true)
	public Product getProductByASIN(String ASIN){
		return dao.getProductByASIN(ASIN);
	}

	@Transactional(readOnly = true)
	public Product getProductByNameAndCategory(String name, int categoryId){
		return dao.getProductByNameAndCategory(name, categoryId);
	}

	@Transactional(readOnly = true)
	public Product getProductName(Product product){
		return dao.getProductName(product);
	}

	@Transactional(readOnly = true)
	public long getNbProduct(){
		return dao.getNbProduct();
	}
	
	@Transactional(readOnly = true)
	public List<Product> getProductsByResponsible(String responsible){
		return dao.getProductsByResponsible(responsible);
	}

	/*************************************
	 * Setter
	 *************************************/

	@Transactional
	public void setResponsible(Product p, String responsible){
		dao.setResponsible(p, responsible);
	}
	
	@Transactional
	public void saveProduct(Product p){
		dao.saveProduct(p);
	}
	
	@Transactional
	public void saveProducts(List<Product> products){
		dao.saveProducts(products);
	}

	/*************************************
	 * Deletion
	 *************************************/

	@Transactional
	public void deleteProduct(String ASIN){
		dao.deleteProduct(ASIN);
	}

	@Transactional
	public void deleteProductsByCategory(int categoryId){
		dao.deleteProductsByCategory(categoryId);
	}

	@Transactional
	public void clearDB() {
		dao.clearDB();
	}
}
